import test
from msg_id import MsgId

### ------------------------------------- data struct ----------------------------------------###
class Account:
    account_name = ''
    uid = 0
    
    def __init__(self):
        pass

### ------------------------------------- base function --------------------------------------###
def mod_name():
    return __name__

def init_mod_data():
    return Account()

def registe_mod(player):
    player.data[mod_name()] = init_mod_data()

def get_data(player:str) -> Account:
    return player.data.get(mod_name(), Account())

def set_data(player, data:Account):
    player.data[mod_name()] = data    

def toc_handler(player, msg_id, content):
    if msg_id == MsgId.MSG_ID_LOGIN.value:
        toc_login(player, content)
    elif msg_id == MsgId.MSG_ID_ENTER_GAME.value:
        test.ramdom_enter(player)
    else:
        print(f'unmatch msg msg_id:{msg_id} content:{content}')

### ------------------------------------- mod function ---------------------------------------###




### ------------------------------------- client to server -----------------------------------###
def tos_login(player, account_name): 
    msg = [('account_name', account_name)]
    account = get_data(player)
    account.account_name = account_name
    set_data(player, account)
    player.send_msg(MsgId.MSG_ID_LOGIN.value, msg)

def tos_enter_game(player): 
    msg = [('uid', get_data(player).uid)]
    player.send_msg(MsgId.MSG_ID_ENTER_GAME.value, msg)

### ------------------------------------- server to client -----------------------------------###

def toc_login(player, content:dict):
    uid = content.get('uid', 0)
    if uid > 0:
        account = get_data(player)
        account.uid = uid
        set_data(player, account)
        player.recv_msg_thread.set_uid(uid)
        player.send_msg_thread.set_uid(uid)
        tos_enter_game(player)

def toc_enter_game(player):
    print(f'uid:{get_data(player).uid} account_name:{get_data(player).account_name} enter game ok')
    map.tos_enter_map(player)
    


